package com.xbfinal.educms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xbfinal.commonUtils.Exception.GuliException;
import com.xbfinal.commonUtils.JwtUtils;
import com.xbfinal.commonUtils.MD5;
import com.xbfinal.educms.entity.UcenterMember;
import com.xbfinal.educms.entity.vo.RegisterVo;
import com.xbfinal.educms.mapper.UcenterMemberMapper;
import com.xbfinal.educms.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author xbfinal
 * @since 2023-02-28
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {


    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 登录方法
     * @param ucenterMember
     * @return
     */
    @Override
    public String login(UcenterMember ucenterMember) {

        //获取邮件号码和密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        //判空
        if(StrUtil.isEmpty(mobile) ||StrUtil.isEmpty(password)){
            throw new GuliException(20001,"号码和密码不能为空");
        }

        //判断号码是否存在
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(ucenterMemberQueryWrapper);

        if(member==null){
            //没有这个手机号
            throw new GuliException(20001,"没有此手机号");
        }


        //判断密码
        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw new GuliException(20001,"密码错误");
        }

        //判断用户是否被禁用
        if(member.getIsDisabled()){
            throw new GuliException(20001,"此用户被禁用");
        }

        //登录成功
        //生成token，使用jwt工具
        String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return jwtToken;
    }

    /**
     * 注册
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        //验证码
        String code = registerVo.getCode();
        //邮件
        String mobile = registerVo.getMobile();
        //昵称
        String nickname = registerVo.getNickname();
        //密码
        String password = registerVo.getPassword();

        if(StrUtil.isEmpty(code)||
                StrUtil.isEmpty(mobile)||
                StrUtil.isEmpty(nickname)||
                StrUtil.isEmpty(password)){
            throw  new GuliException(20001,"请填写完整  注册失败");
        }


        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw  new GuliException(20001,"验证码错误  注册失败");
        }
        //判断手机号是否重复
        QueryWrapper<UcenterMember> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(memberQueryWrapper);
        if(integer>0){
            throw  new GuliException(20001,"账号已经存在  注册失败");
        }
        //成功添加到数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");

        int insert = baseMapper.insert(ucenterMember);
        if(insert<1){
            throw  new GuliException(20001,"服务器异常 注册失败");
        }
    }

    /**
     * 微信登录
     * @param openid
     * @return
     */

    @Override
    public UcenterMember getOpenIdMember(String openid) {

        QueryWrapper<UcenterMember> Wrapper = new QueryWrapper<>();
        Wrapper.eq("openid",openid);

        UcenterMember ucenterMember = baseMapper.selectOne(Wrapper);
        return ucenterMember;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
